const https = require('https');
const fs = require('fs');

var url = 'https://www.baidu.com/img/bd_logo1.png';

https.get(url, (res) => {

    var imgData = "";
    res.setEncoding("binary");  // 下载图片需要设置为 binary, 否则图片会打不开

    res.on('data', (chunk) => {
       imgData+=chunk;
    });

    res.on('end', () => {
        fs.writeFileSync("./download.png", imgData, "binary");
        console.log('ok');
    });
});